// Package clients implements mqtt and HTTP client initialization and message processing
package clients

import (
	"k8s.io/klog/v2"
	"net/http"
	"sync"
	"time"
)

// HttpClient is structure used to init HttpClient
type HttpClient struct {
	IP string
	Port string
	WriteTimeout time.Duration
	ReadTimeout time.Duration
}

// InitHttpClient is a method to construct HTTP server
func (hc *HttpClient)InitHttpClient(wg *sync.WaitGroup) error{
	var err error
	mux := http.NewServeMux()
	initRouter(mux)
	server := &http.Server{
		Addr:         hc.IP+":"+hc.Port,
		WriteTimeout: hc.WriteTimeout,
		ReadTimeout: hc.ReadTimeout,
		Handler:      mux,
	}
	klog.V(1).Info("HttpServer Start......")
	wg.Add(1)
	go func() {
		defer wg.Done()
		err = server.ListenAndServe()
		if err != nil {
			if err == http.ErrServerClosed {
				klog.Error("Server closed under request")
				return
			} else {
				klog.Error("Server closed unexpected")
				return
			}
		}
		klog.Error("Server exited")
		return
	}()
	return err
}



